package com.yanqu.road.web.controller.usercenter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.servercenter.ServerAutoOpenConfigDaoImpl;
import com.yanqu.road.entity.servercenter.ServerAutoOpenConfig;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.server.manger.AutoOpenServerMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet(urlPatterns = "/consoleServerRoleType")
public class ConsoleServerRoleType extends BaseJsonHttpServlet {

	private static Logger logger = LogManager.getLogger(ConsoleServerRoleType.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("request consoleServerRoleType");
		HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
		JSONObject paramObj = httpJsonWebParam.getParamObj();
		int channelId = 0;
		int type = 1 ;
		int level = 2;
		String areaSplitFlow = "";
		try{
			channelId = paramObj.getIntValue("channelId");
			type = paramObj.getIntValue("type");
			level = paramObj.getIntValue("level");
			if(paramObj.containsKey("areaSplitFlow")){
				areaSplitFlow = paramObj.getString("areaSplitFlow");
			}
		}catch (Exception e){
			logger.error("consoleServerRoleType argument error:" + e);
			sendResult(response, 0, "argument error");
			return;
		}

		ServerAutoOpenConfig serverAutoOpenConfig = AutoOpenServerMgr.initServerAutoOpenConfig(channelId, type, level, areaSplitFlow);
		new ServerAutoOpenConfigDaoImpl().updateData(serverAutoOpenConfig);
		AutoOpenServerMgr.setAutoOpenConfig(channelId, serverAutoOpenConfig);

		List<ServerInfo> serverList = ServerListMgr.getSplitFlowServerList();
		JSONObject resultObj = new JSONObject();
		resultObj.put("code", 0);
		resultObj.put("msg", "成功");
		resultObj.put("data", new JSONArray());
		for (ServerInfo serverInfo : serverList) {
			if(serverInfo.getChannelId() != channelId){
				continue;
			}
			long serverId = serverInfo.getServerId();
			String serverData = "";
			try {
				serverData = HttpHelper.doPostJsonTimeOut(serverInfo.getWebServerUrl() + "/serverRoleType", paramObj.toJSONString(), "UTF-8", new HashMap<>());
				JSONObject jsonObj = JSONObject.parseObject(serverData);
				if(null != jsonObj && jsonObj.containsKey("code")) {

				}else {
					resultObj.getJSONArray("data").add(serverId);
				}
			} catch (Exception ex) {
				logger.error("json parse error,{},error:", serverData, ex);
			}

		}
		sendMessage(response, resultObj.toJSONString());
	}

}
